package com.job;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.entity.DinerOrder;
import com.service.DinerOrderService;
import com.service.impl.DinerOrderServiceImpl;
import com.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.Date;
import java.util.List;

import static com.service.impl.DinerOrderServiceImpl.getRandom;

@Component
public class DinerJob {

    @Autowired
    DinerOrderService dinerOrderService;


    /**
     * 当时修改订单状态
     */
    @Scheduled(cron = "0 0/2 * * *   ? ")
    public void fangfaming() {
        List<DinerOrder> list = dinerOrderService.getDinerOrderpayInfo();
        DinerOrder dinerOrder = new DinerOrder();
        for (DinerOrder dinerOrder1 : list) {
            dinerOrder.setId(dinerOrder1.getId());
            switch (dinerOrder1.getStatus()) {

                case 1:
                    dinerOrder.setStatus(3);
                    break;
//                case 2:
//                    dinerOrder.setStatus(3);
//                    break;
                case 3:
                    dinerOrder.setStatus(4);
                    break;
                case 4:
                    dinerOrder.setStatus(5);
                    break;
                default:
                    dinerOrder.setStatus(dinerOrder1.getStatus());
                    break;
            }

            dinerOrderService.UpdateDinerrOrderState(dinerOrder);
        }

    }

    /**
     * 修改订单支付状态
     */
    @Scheduled(cron = "0/1 * * * * ? ")
    public void updateefPayStatusInfo() throws ParseException {
        List<DinerOrder> list = dinerOrderService.getDinerOrderInfoStatus();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        for (DinerOrder dinerOrder : list) {
            String time = dinerOrder.getCreateTime();
            Date date2 = DateUtil.parse(time);
            long d = DateUtil.between(date, date2, DateUnit.MINUTE);
            if(d==3){
                DinerOrder dinerOrder1=new DinerOrder();
                dinerOrder1.setStatus(2);
                dinerOrder1.setDelFlag('1');
                dinerOrder1.setId(dinerOrder.getId());
               dinerOrderService.UpdateDinerrOrderState(dinerOrder1);
            }
        }

    }


}
